#pragma once 
#include<iostream>
#include<string>
#include<pthread.h>
#include<functional>
namespace ThreadModule
{
    using func_t = std::function<void(std::string)>;
    class Thread
    {
        public:
            void Excute()
            {
                _func(_threadname);
            }
        public:
            Thread(func_t func,std::string name = "none-name")
            :_func(func),
            _threadname(name)
            {}
            static void* threadroutime(void* args)
            {
                Thread* self = static_cast<Thread*>(args);
                self->Excute();
                return nullptr;
            }
            bool Start()
            {
                int n = pthread_create(&_tid,nullptr,threadroutime,this);
                if(!n)
                {
                    _stop = false;
                    return true;
                }
                else 
                {
                    return false;
                }
            }
            //线程分离
            void Detach()
            {
                if(!_stop)
                {
                    pthread_detach(_tid);
                }
            }
            void Join()
            {
                if(!_stop)
                {
                    pthread_join(_tid,nullptr);
                }
            }
            std::string Name()
            {
                return _threadname;
            }
            void Stop()
            {
                _stop = true;
            }
            ~Thread()
            {
            }
        private:
            pthread_t _tid;
            std::string _threadname;
            func_t _func;
            bool _stop;
    };
}